package com.huaruan.youdi.views.activitys;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.huaruan.youdi.R;
import com.huaruan.youdi.adapter.AppealListAdapter;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpPageUtils;
import com.huaruan.youdi.models.AppealModel;
import com.huaruan.youdi.utils.AppUtils;
import com.huaruan.youdi.utils.DeviceUtils;
import com.huaruan.youdi.utils.PreferenceUtil;
import com.huaruan.youdi.views.widget.easyrecyclerview.EasyRecyclerView;
import com.huaruan.youdi.views.widget.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.huaruan.youdi.views.widget.easyrecyclerview.decoration.SpaceDecoration;

import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;

public class AppealListActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;

    AppealListAdapter appealListAdapter;
    HttpPageUtils httpPageUtils;

    @Override
    public int IGetMultiSatesContentViewResId() {
        return R.layout.activity_list;
    }

    @Override
    public int IGetContentViewResId() {
        return 0;
    }

    @Override
    public void IFindViews() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(IGetContext(),
                LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        SpaceDecoration itemDecoration = new SpaceDecoration(DeviceUtils.dp2px(5));
        recyclerView.addItemDecoration(itemDecoration);
        appealListAdapter = new AppealListAdapter(IGetActivity());
        recyclerView.setAdapter(appealListAdapter);

        appealListAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                AppealModel model = appealListAdapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("model", model);
                AppUtils.jump(IGetContext(), AppealDetailActivity.class, bundle);
            }
        });

        appealListAdapter.setMore(R.layout.loadmore, new RecyclerArrayAdapter.OnMoreListener() {
            @Override
            public void onMoreShow() {
                httpPageUtils.next();
            }

            @Override
            public void onMoreClick() {

            }
        });

    }

    @Override
    public void IInitData() {

    }

    @Override
    public void IRequest() {
        findByPhoneAll();
    }

    private void findByPhoneAll() {
        SortedMap<String, String> sortMap = new TreeMap<>();
        sortMap.put("tel", PreferenceUtil.getString(PreferenceUtil.PreferenceKeys.PHONE, ""));
        httpPageUtils = new HttpPageUtils(IGetContext())
                .url(Constant.GET_APPEAL_LIST)
                .params(sortMap)
                .modelClass(AppealModel.class)
                .pageSize(10)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object o) {
                        hideLoadingAndRetryView();
                        if (httpPageUtils.isFirstPage()) {
                            appealListAdapter.clear();
                        }
                        ArrayList<AppealModel> models = (ArrayList<AppealModel>) o;
                        if (httpPageUtils.isFirstPage() && models.size() == 0) {
                            AppUtils.toastShort(IGetContext(), "暂无接单");
                            return;
                        } else {
                            appealListAdapter.addAll(models);
                        }
                    }

                    @Override
                    public void failure(int status, String errorMsg) {
                        super.failure(status, errorMsg);
                        hideLoadingAndRetryView();
                    }
                });
        httpPageUtils.async2();

    }

}
